package com.example.demo.controller;

import com.example.demo.entity.Commodity;
import com.example.demo.service.CommodityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.sql.PreparedStatement;
import java.util.List;

@Api("Commodity manager")
@RestController
@RequestMapping("/commodity")
public class CommodityController {
    @Autowired
    CommodityService service;

    @ApiOperation("Get commodity by id")
    @GetMapping("/{id}")
    public ResponseEntity<Commodity> getCommodity(@ApiParam("Commodity ID") @PathVariable long id){
        Commodity result = service.getCommodity(id);
        if(result == null)
            return ResponseEntity.noContent().build();
        else
           return ResponseEntity.ok(result);
    }

    @ApiOperation("Find commodity by name")
    @GetMapping("")
    public ResponseEntity<List<Commodity>> findCommodity(@ApiParam("Commodity name")String commodityName,
                                                         @ApiParam("Commodity name")String supplierName){
        List<Commodity> result = service.findCommodity(commodityName,supplierName);
        return ResponseEntity.ok(result);
    }

    @ApiOperation("Add commodity infos")
    @PostMapping("")
    public ResponseEntity<Commodity> addCommodity(@RequestBody Commodity commodity){
        Commodity result = service.addCommodity(commodity);
        return ResponseEntity.ok(result);
    }

    @ApiOperation("Update Commodity info")
    @PutMapping("/{id}")
    public ResponseEntity<Void> updateCommodity(@PathVariable long id,@RequestBody Commodity commodity){
        service.updateCommodity(commodity);
        return ResponseEntity.ok().build();
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteCommodity(@PathVariable long id){
        service.deleteCommodity(id);
        return ResponseEntity.ok().build();
    }
}
